package com.dayuanit.dy17.atm.atmbackoffice.task;

import com.dayuanit.dy17.atm.atmbackoffice.entity.Salary;
import com.dayuanit.dy17.atm.atmbackoffice.enums.SalaryStatusEnum;
import com.dayuanit.dy17.atm.atmbackoffice.mapper.SalaryMapper;
import com.dayuanit.dy17.atm.atmbackoffice.service.ISalaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SalaryTask extends BaseTask {

    private static final Logger logger = LoggerFactory.getLogger("com.dayuanit.dy17.atm.atmbackoffice.task");

    @Autowired
    private SalaryMapper salaryMapper;

    @Autowired
    private ISalaryService salaryService;

    @Scheduled(cron = "0/5 * * * * ?")
    @Override
    public void doTask() {
        processTask();
    }

    private void processTask() {
        logger.info("-------SalaryTask processTask()--------");

        List<Salary> salaries = salaryMapper.listSalary(SalaryStatusEnum.success_approval.getK());
        for (Salary salary : salaries) {
            try {
                salaryService.payOff(salary);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}
